package tianrun.ziguan.api.calculate.config.dto.response;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import tianrun.ziguan.api.calculate.config.entity.TAssetCalLabelConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class AssetLabelRespDetail extends AssetCommonResp {

    private String calLabel;
    private String type;

    public String getCalLabel() {
        return calLabel;
    }

    public void setCalLabel(String calLabel) {
        this.calLabel = calLabel;
    }

    public String getType() {
        return type;
    }

    public AssetLabelRespDetail setType(String type) {
        this.type = type;
        return this;
    }

    public static Map<String,List<AssetLabelRespDetail>> parse(TAssetCalLabelConfigVO labelResp, Map<String, List<String>> assetNumMap) {
        Map<String,List<AssetLabelRespDetail>> resultMap = Maps.newHashMap();
        ArrayList<String> emptyList = Lists.newArrayList();
        // TODO: 2023/9/25 该参数待拓展
        String assetNum = labelResp.getAssetNum();
        String calLabel = labelResp.getCalLabel();
        if (TAssetCalLabelConfig.SELF.equals(labelResp.getSource())) {
            //自身
            resultMap.put(assetNum,Lists.newArrayList(of(assetNum, calLabel,"indicators")));
        } else if (TAssetCalLabelConfig.CHILD.equals(labelResp.getSource())){
            //如果是子设备,则存父设备编码
            assetNumMap.getOrDefault(assetNum, emptyList).forEach(childAssetNum -> {
                resultMap.computeIfAbsent(childAssetNum,s -> Lists.newArrayList()).add(of(assetNum, calLabel,"indicators"));
            });
        }else if (TAssetCalLabelConfig.PROGENY.equals(labelResp.getSource())){
            //孙子设备
            assetNumMap.getOrDefault(assetNum, emptyList).forEach(childAssetNum -> {
                assetNumMap.getOrDefault(childAssetNum,emptyList).forEach(progenyAssetNum -> {
                    resultMap.computeIfAbsent(progenyAssetNum,s -> Lists.newArrayList()).add(of(assetNum, calLabel,"indicators"));
                });
            });
        }
        return resultMap;
    }

    private static AssetLabelRespDetail of(String assetNum,String calLabel,String type) {
        AssetLabelRespDetail calLabelResp = new AssetLabelRespDetail();
        calLabelResp.setType(type);
        calLabelResp.setAssetNum(assetNum);
        calLabelResp.setCalLabel(calLabel);
        return calLabelResp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AssetLabelRespDetail that = (AssetLabelRespDetail) o;
        return Objects.equals(calLabel, that.calLabel) && Objects.equals(type, that.type);
    }

    @Override
    public int hashCode() {
        return Objects.hash(calLabel, type);
    }
}
